﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Domain.Concrete
{
    public class Status : DefaultDisposable, IStatus
    {
        private readonly Table<StatusEntity> _statusTable;

        public Status()
        {
            _statusTable = context.GetTable<StatusEntity>();
        }

        public List<StatusEntity> GetAllStatus()
        {
            return _statusTable.OrderByDescending(c => c.CreateDate).ToList();
        }

        //public List<StatusEntity> GetStatusNew()
        //{

        //}

        public List<StatusEntity> GetStatusByUserID(int userID)
        {
            return _statusTable.Where(c => c.OwnerUserID == userID).OrderByDescending(c => c.CreateDate).ToList();
        }

        public StatusEntity GetStatusByStatusID(int statusID)
        {
            return _statusTable.Where(c => c.StatusID == statusID).FirstOrDefault();
        }

        public void AddStatus(StatusEntity statusEntity)
        {
            statusEntity.CreateDate = DateTime.Now;
            _statusTable.InsertOnSubmit(statusEntity);
            context.SubmitChanges();
        }

        public void UpdateStatus(StatusEntity statusEntity)
        {
            var status = _statusTable.SingleOrDefault(c => c.StatusID == statusEntity.StatusID);
            if (statusEntity != null)
            {
                status.Status = statusEntity.Status;
                status.UpdateDate = statusEntity.UpdateDate;
                context.SubmitChanges();
            }
        }

        public void DeleteStatusByStatusID(int statusID)
        {
            var status = _statusTable.SingleOrDefault(c => c.StatusID == statusID);
            if (status != null)
            {
                _statusTable.DeleteOnSubmit(status);
                context.SubmitChanges();
            }
        }

        ~Status()
        {
            Dispose(false);
        }
    }
}
